package com.nulldev.util.security.hashAPI.impls.sha256;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import com.nulldev.util.VariableAPI.ByteUtil;
import com.nulldev.util.security.hashAPI.Hashable;

public class SHA256 implements Hashable<String>, Cloneable {

	private final MessageDigest digest;

	public SHA256() {
		try {
			this.digest = MessageDigest.getInstance("SHA-256");
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException(e);
		}
	}

	public SHA256(boolean nullInit) {
		this.digest = null;
	}

	@Override
	public String hash(byte[] data) {
		final String s = ByteUtil.getHex(digest.digest(data));
		digest.reset();
		return s;
	}

	@Override
	public byte[] raw_hash(byte[] data) {
		final byte[] d = digest.digest(data);
		digest.reset();
		return d;
	}

	@Override
	public String id() {
		return "native.sha256";
	}

	@Override
	public SHA256 clone() {
		return new SHA256();
	}

	@Override
	public String[] hashes() {
		return new String[]
			{ "SHA256", "SHA-256" };
	}

}
